﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using GraphosData;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace GraphosControls
{
	public class VisualEdge : Control
	{


		public VisualEdge(VisualNode start, VisualNode end, Canvas parent)
		{
			m_locationAnim.Completed += new EventHandler(Destroy_Completed);

			DataContext = this;

			m_startVisualNode = start;
			m_endVisualNode = end;
			m_parentCanvas = parent;

			// Bind the start point and end point to the visual nodes
			Binding startBinding = new Binding("Location");
			startBinding.Source = m_startVisualNode;
			SetBinding(VisualEdge.StartPointProperty, startBinding);

			Binding endBinding = new Binding("Location");
			endBinding.Source = m_endVisualNode;
			SetBinding(VisualEdge.EndPointProperty, endBinding);

			m_parentCanvas.Children.Add(this);
		}

		#region Dependecy Properties

		public Point StartPoint
		{
			get { return (Point)GetValue(StartPointProperty); }
			set { SetValue(StartPointProperty, value); }
		}
		public static readonly DependencyProperty StartPointProperty =
			 DependencyProperty.Register("StartPoint", typeof(Point), typeof(VisualEdge), new FrameworkPropertyMetadata(new Point(0, 0)));

		public Point EndPoint
		{
			get { return (Point)GetValue(EndPointProperty); }
			set { SetValue(EndPointProperty, value); }
		}
		public static readonly DependencyProperty EndPointProperty =
			 DependencyProperty.Register("EndPoint", typeof(Point), typeof(VisualEdge), new FrameworkPropertyMetadata(new Point(0, 0)));

		#endregion

		public void DestroyWithAnimatin()
		{

			m_destroyAnim.Freeze();
			var one = m_locationAnim.Clone();
			var two = m_locationAnim.Clone();
			BeginAnimation(OpacityProperty, m_destroyAnim);

			one.To = new Point(Rand.Double * m_parentCanvas.ActualWidth, Rand.Double * m_parentCanvas.ActualHeight);
			one.Freeze();
			//m_locationAnim.To = new Point(Rand.Double * m_parentCanvas.ActualWidth, Rand.Double * m_parentCanvas.ActualHeight);
			BeginAnimation(StartPointProperty, one);

			two.To = new Point(Rand.Double * m_parentCanvas.ActualWidth, Rand.Double * m_parentCanvas.ActualHeight);
			two.Freeze();
			//m_locationAnim.To = new Point(Rand.Double * m_parentCanvas.ActualWidth, Rand.Double * m_parentCanvas.ActualHeight);
			BeginAnimation(EndPointProperty, two);

			m_startVisualNode = null;
			m_endVisualNode = null;
			m_locationAnim = null;
		}
		private void Destroy_Completed(object sender, EventArgs e)
		{
			

			m_parentCanvas.Children.Remove(this);
			//m_parentCanvas = null;
		}
		private VisualNode m_startVisualNode;
		private VisualNode m_endVisualNode;
		private Canvas m_parentCanvas;

		private DoubleAnimation m_destroyAnim = new DoubleAnimation() { To = 0.0, Duration = new Duration(TimeSpan.FromSeconds(1.0)) };
		private PointAnimation m_locationAnim = new PointAnimation() { To = new Point(0, 0), Duration = new Duration(TimeSpan.FromSeconds(1.0)) };

	}
}
